﻿#region Document change records 
// UrlParser.cs
// Created by Raojie at 2017/06/13  13:45
#endregion

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace IiiToolkitMain.Common
{
    public static class UrlParser
    {
        static Regex urlRegex = new Regex(@"^ewind://(?<type>[a-zA-Z]+)(?<xx>[?/]{1})(?<params>.*)");

        public static UrlModel ParseUrl(string url)
        {
            UrlModel model = new UrlModel();
            var match = urlRegex.Match(url);
            if (!match.Success)
            {
                model.Type = AppType.Error;
                return model;
            }

            var type = match.Groups["type"];
            if (type.Success)
            {
                switch (type.Value.ToLower())
                {
                    case "video":
                        model.Type = AppType.Video;
                        break;
                    case "innervideo":
                        model.Type = AppType.InnerVideo;
                        break;
                    default:
                        model.Type = AppType.Other;
                        break;

                }
            }
            var queryString = match.Groups["params"];
            if (queryString.Success)
            {
                model.Params = new Dictionary<string, string>();
                var para = queryString.Value.TrimStart('?').Split('&');
                foreach (var p in para)
                {
                    if (string.IsNullOrEmpty(p)) continue;
                    var kv = p.Split('=');
                    if (kv.Length != 2) continue;
                    model.Params.Add(kv[0], kv[1]);
                }
            }
            return model;
        }
    }
}